import React, { Component } from 'react'

export default class Demo extends Component {
    state = {
        count: 0
    }
    add = () => {
        // const { count } = this.state

        // 语法糖--对象式
        // this.setState({ count: count + 1 })
        
        // 函数式
        this.setState(state => ({ count: state.count + 1 }))
    }
    
    handleClick = () => {
        this.setState(state => ({ count: state.count + 1 }))
        console.log(this.state.count) // 1
    
        this.setState(state => ({ count: state.count + 1 }))
        console.log(this.state.count) // 1
    
        this.setState(state => ({ count: state.count + 1 }))
        console.log(this.state.count) // 1
    }

    render() {
        const { count } = this.state
        return (
            <div>
                <h2>当前求和为：{count}</h2>
                <button onClick={this.handleClick}>点击加1</button>
            </div>
        )
    }
}
